package Pet.Management.common.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class RedisUtils {
    
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    
    private final String CAPTCHA_CODE_KEY = "captcha_codes:";
    
    public void setCaptchaCode(String uuid, String code) {
        String key = CAPTCHA_CODE_KEY + uuid;
        redisTemplate.opsForValue().set(key, code, 2, TimeUnit.MINUTES); // 验证码2分钟有效
    }
    
    public String getCaptchaCode(String uuid) {
        String key = CAPTCHA_CODE_KEY + uuid;
        Object code = redisTemplate.opsForValue().get(key);
        return code != null ? code.toString() : null;
    }
    
    public void deleteCaptchaCode(String uuid) {
        String key = CAPTCHA_CODE_KEY + uuid;
        redisTemplate.delete(key);
    }
} 